<template>
  <div>
    <!--    查询功能-->
    <div style="margin-bottom: 5px">
      <el-button type="primary" style="margin-left: 5px" @click="queryAll">查询</el-button>
      <!--      新增按钮-->
      <el-button type="primary" style="margin-left: 5px" @click="add">新增</el-button>
    </div>

    <!--    列表展示-->
    <el-table :data="departmentalRole" :header-cell-style="{ background: '#D3D3D3FF', color: '#555555' }">
      <el-table-column prop="id" label="部门角色ID" width="180"> </el-table-column>
      <el-table-column prop="role" label="部门角色名称" width="160"> </el-table-column>
      <el-table-column prop="gmtCreate" label="创建时间" width="180">
      </el-table-column>
      <el-table-column prop="gmtModified" label="修改时间" width="180">
      </el-table-column>
      <el-table-column prop="operate" label="操作">
        <template slot-scope="scope">
          <el-button size="small" type="success" @click="mod(scope.row)">编辑</el-button>
          <el-popconfirm confirm-button-text="确定" cancel-button-text="取消" icon="el-icon-info" icon-color="red"
            title="这是一段内容确定删除吗？" @confirm="del(scope.row.id)" style="margin-left: 5px">
            <el-button slot="reference" size="small" type="danger">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页  -->
    <pagination style="text-align: right;" v-show="total > 0" :pageSizes="[5, 10, 20, 30]" :total="total" :page.sync="queryData.pageNum"
      :limit.sync="queryData.pageSize" @pagination="getList" />



    <el-dialog title="部门角色信息" :visible.sync="centerDialogVisible" width="30%" center :before-close="handleClose">
      <!--      新增部门角色信息表单-->
      <el-form ref="form" :rules="rules" :model="form" label-width="120px">
        <el-form-item label="部门角色名称" prop="role">
          <el-col :span="15">
            <el-input v-model="form.role"></el-input>
          </el-col>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { queryAll, mod, add, del } from "@/api/departmentalServices/departmentalRoleManagement";
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "departmentalRoleManagement",
  data() {
    return {
      departmentalRole: [],
      centerDialogVisible: false,
      form: {
        id: "",
        role: "",
        clubId: "",
      },
      rules: {
        role: [{ required: true, message: "请输入姓名", trigger: "blur" }],
      },
      total: 0,
      queryData: {
        pageNum: 1,
        pageSize: 5
      }

    };
  },
  methods: {
    //重置表单
    resetForm() {
      this.$refs.form.resetFields();
      this.form = {
        id: "",
        role: "",
        clubId: "",
      }
      //将计数器的数字重置为1
      this.num = 1;
    },

    //新增按钮
    add() {
      this.centerDialogVisible = true;
      this.$nextTick(() => {
        this.resetForm();
      });
    },
    //修改
    mod(row) {
      this.centerDialogVisible = true;
      this.$nextTick(() => {
        //赋值到表单
        this.form.id = row.id
        this.form.name = row.name
        this.form.clubId = row.clubId
        this.form.sort = row.sort
      });
    },
    //删除
    del(id) {
      del(id).then((res) => {
        if (res.code === 20000) {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.centerDialogVisible = false;
          this.queryAll();
        } else {
          this.$message({
            message: "删除失败",
            type: "error",
          });
        }
      });
    },
    doMod() {
      mod(this.form.id, this.form).then((res) => {
        if (res.code === 20000) {
          this.$message({
            message: "修改成功",
            type: "success",
          });
          this.centerDialogVisible = false;
          this.queryAll();
          this.resetForm();
        } else {
          this.$message({
            message: "修改失败",
            type: "error",
          });
        }
      });
    },
    doSave() {
      add(this.form).then((res) => {
        if (res.code === 20000) {
          this.$message({
            message: "增加成功",
            type: "success",
          });
          this.centerDialogVisible = false;
          this.queryAll();
          this.resetForm();
        } else {
          this.$message({
            message: "增加失败",
            type: "error",
          });
        }
      });
    },
    //提交数据到后台
    save() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.form.id) {
            //如果有id则修改
            this.doMod();
          } else {
            //否则增加
            this.doSave();
          }
        } else {
          this.$message({
            message: "操作失败",
            type: "error",
          });
          return false;
        }
      });
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(() => {
          done();
        })
        .catch(() => { });
    },
    resetParam() {
      this.queryAll();
    },
    queryAll() {
      queryAll(this.queryData.pageNum,
        this.queryData.pageSize).then((res) => {
          if (res.code === 20000) {
            this.departmentalRole = res.data.list.records;
            this.total = res.data.list.total
          } else {
            alert("获取数据失败");
          }
        });
    },
    getList(e) {
      this.queryData.pageNum = e.page
      this.queryData.pageSize = e.limit
      this.queryAll()
    }
  },
  beforeMount() {
    this.queryAll();
  },
};
</script>

<style scoped>
.pagination-wrapper {
  position: fixed;
  bottom: 0;
  width: 100%;
  /*background-color: #f5f5f5;*/
  padding: 10px 20px;
  /*box-shadow: 0 -2px 4px rgba(0, 0, 0, 0.1);*/
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pagination-container {
  max-width: 800px;
  /* 设置最大宽度以避免在大屏幕上过分拉伸 */
  margin: 0 30%;
  /* 居中显示分页组件 */
}
</style>